package com.i2works.smartluolong.web.service.user.impl;


import com.i2works.smartluolong.web.service.user.IUserMenuService;
import com.i2works.smartluolong.utils.basecommon.BaseCommonServiceImpl;
import com.i2works.smartluolong.utils.exception.DAOException;
import com.i2works.smartluolong.utils.exception.ServiceException;
import com.i2works.smartluolong.web.dal.user.dao.IUserMenuDAO;
import com.i2works.smartluolong.web.entity.user.UserMenuDO;
import com.i2works.smartluolong.web.entity.user.UserMenuQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 会员菜单业务实现类
 *
 * @author Liu Yutao IUserMenuService
 * @email koal@vip.qq.com
 * @date 2016/2/26 16:47
 */
@Service("userMenuService")
public class UserMenuServiceImpl  extends BaseCommonServiceImpl<UserMenuQuery,UserMenuDO> implements IUserMenuService {

    @Autowired
    private IUserMenuDAO userMenuDAO;

    /**
     * 查询菜单列表
     */
    @Override
    public List<UserMenuDO> queryList(UserMenuQuery query) throws ServiceException {
        try {
            return userMenuDAO.queryList(query);
        } catch (DAOException e) {
            throw new ServiceException(e);
        }
    }

    /**
     * 查询菜单列表统计
     */
    @Override
    public Integer queryCount(UserMenuQuery query) throws ServiceException {
        try {
            return userMenuDAO.queryCount(query);
        } catch (DAOException e) {
            throw new ServiceException(e);
        }
    }

    /**
     * 无分页菜单列表
     */
    @Override
    public List<UserMenuDO> queryAllList(UserMenuQuery query) throws ServiceException {
        try {
            return userMenuDAO.queryAllList(query);
        } catch (DAOException e) {
            throw new ServiceException(e);
        }
    }

    /**
     * 添加系统菜单
     */
    @Override
    public void save(UserMenuDO userMenuDO) throws ServiceException {
        try {
            if (userMenuDO.getId() != null && userMenuDO.getId() != 0) {
                userMenuDAO.update(userMenuDO);
            } else {
                userMenuDO.setCreateName(userMenuDO.getUpdateName());
                userMenuDAO.save(userMenuDO);
            }
        } catch (DAOException e) {
            throw new ServiceException(e);
        }
    }


    /**
     * 查询一个菜单数据
     */
    @Override
    public UserMenuDO query(UserMenuQuery query) throws ServiceException {
        try {
            return userMenuDAO.query(query);
        } catch (DAOException e) {
            throw new ServiceException(e);
        }
    }


    /**
     * 验证菜单名称、标示、代码是否存在
     */
    @Override
    public Integer queryCheck(UserMenuQuery query) throws ServiceException {
        try {
            return userMenuDAO.queryCheck(query);
        } catch (DAOException e) {
            throw new ServiceException(e);
        }
    }

    /**
     * 更新菜单状态
     */
    @Override
    public void updateStatus(UserMenuDO userMenuDO) throws ServiceException {
        try {
            if (null != userMenuDO) {
                userMenuDAO.update(userMenuDO);
                //如果是父类，关联设置其子类状态
                if (userMenuDO.getPid() == 0) {
                    UserMenuQuery query = new UserMenuQuery();
                    query.setPid(userMenuDO.getId());
                    //查询其所有子类
                    List<UserMenuDO> childrenResList = userMenuDAO.queryAllList(query);
                    //循环输出子类，并设置其status为父类status
                    for (UserMenuDO childrenMenu : childrenResList) {
                        childrenMenu.setStatus(userMenuDO.getStatus());
                        //更新子类
                        userMenuDAO.update(childrenMenu);
                    }
                }
            }

        } catch (DAOException e) {
            throw new ServiceException(e);
        }

    }


    /**
     * 批量更改状态
     */
    @Override
    public void updateBatchStatus(Map map) throws ServiceException {
        try {
            userMenuDAO.updateBatchStatus(map);
        } catch (DAOException e) {
            throw new ServiceException(e);
        }
    }

    @Override
    public void delete(Integer id) throws ServiceException {
        try {
            userMenuDAO.delete(id);
        } catch (DAOException e) {
            throw new ServiceException(e);
        }
    }

    @Override
    public void deleteBatch(List<Integer> idsList) throws ServiceException {
        try {
            userMenuDAO.deleteBatch(idsList);
        } catch (DAOException e) {
            throw new ServiceException(e);
        }
    }
}
